package list;

public class MyArrayList {
    public String[] data = null;
    public int size = 0;

    public MyArrayList(){
        data = new String[10];
        size = 0;
    }
    public MyArrayList(int capacity){
        if (capacity <= 10){
            capacity = 10;
        }
        data = new String[capacity];
    }

    public void resize(){
        String[] newData = new String[(int)(data.length * 1.5)];
        for (int i = 0; i < data.length; i++){
            newData[i] = data[i];
        }
        data = newData;
    }

    //尾插
    public void add(String elem){
        if (size >= data.length){
            resize();
        }
        data[size] = elem;
        size++;
    }
    //指定下标插入
    public void add(int index, String elem){
        //判断是否下标合法
        if (index < 0 || index > size){
            throw new IndexOutOfBoundsException("Index:" + index + ",size" + size);
        }
        //判断是否需要扩容
        if (size >= data.length){
            resize();
        }
        //将插入位置后面的数据往后移动
        for (int i = size - 1; i >= index; i--){
            data[i + 1] = data[i];
        }
        data[index] = elem;
        size++;
    }
    //按照下标删除元素
    public String remove(int index){
        if (index < 0 || index >= size){
            throw new IndexOutOfBoundsException("Index:" + index + ",size" + size);
        }
        String elem = data[index];
        //搬运
        for (int i = index;i < size - 1; i++){
            data[i] = data[i + 1];
        }
        size--;
        return elem;
    }
    //按照值来删除元素
    public boolean remove(String elem){
        //判断数据中是否存在elem
        int removePos = 0;
        for (removePos = 0; removePos < size ;removePos++){
            if (elem.equals(data[removePos])){
                break;
            }
        }
        //来到这里有两种情况 1.找到了   2.没找到
        if (removePos == size){
            return false;
        }
        remove(removePos);
        return true;
    }
    public String get(int index){
        if (index < 0 || index >= size){
            throw new IndexOutOfBoundsException("Index:" + index + ",size" + size);
        }
        return data[index];
    }
    public void set(int index,String elem){
        if (index < 0 || index >= size){
            throw new IndexOutOfBoundsException("Index:" + index + ",size" + size);
        }
        data[index] = elem;
    }
    public boolean contains(String elem){
        for (int i = 0; i < size; i++){
            if (data[i].equals(elem)){
                return true;
            }
        }
        return false;
    }
    public int indexOf(String elem){
        for (int i = 0; i < size; i++){
            if (data[i].equals(elem)){
                return i;
            }
        }
        return -1;
    }
    public int lastIndexOf(String elem){
        for (int i = size - 1; i >= 0; i--){
            if (data[i].equals(elem)){
                return i;
            }
        }
        return -1;
    }
    public MyArrayList sublist(int fromIndex, int toIndex){
        //判断合法性
        if(fromIndex < 0 || toIndex > size || fromIndex > toIndex){
            throw new IndexOutOfBoundsException("formIndex:" + fromIndex + ", toIndex" +  toIndex + ", size" + size);
        }
        //创建新的sublist来存储
        MyArrayList sublist = new MyArrayList(toIndex - fromIndex);
        for (int i = fromIndex; i < toIndex; i++){
            String elem = data[i];
            sublist.add(elem);
        }
        return sublist;
    }
    public void clean(){
        size = 0;
    }

    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < size; i++){
            stringBuilder.append(data[i]);
            if (i < size - 1){
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
    public static void test1(){
        MyArrayList list = new MyArrayList();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");
        list.add("11");
        System.out.println(list);
    }
    public static void test2(){
        MyArrayList list = new MyArrayList();
        list.add(0,"1");
        list.add(0,"2");
        list.add(0,"3");
        list.add(0,"4");
        list.add(4,"5");
//        list.add(6,"6");
        System.out.println(list);
    }
    public static void test3(){
        MyArrayList list = new MyArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        String elem = list.remove(1);
        System.out.println(list);
        System.out.println(elem);
    }
    public static void test4(){
        MyArrayList list = new MyArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        boolean ret = list.remove("a");
        System.out.println(ret);
        System.out.println(list);

    }
    public static void test5(){
        MyArrayList list = new MyArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        System.out.println(list.contains("x"));
    }
    public static void test6(){
        MyArrayList list = new MyArrayList();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");
        list.add("11");
        System.out.println(list.sublist(4,9));
    }
    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
        test6();
    }
}
